#Boa:Dialog:Dialog1

import wx
import wx.grid


CONFIG_BATTLE_FIELD_HOR_SIZE = 10
CONFIG_BATTLE_FIELD_VER_SIZE = 10

def create(parent, app ):
    return Dialog1(parent, app )

[wxID_DIALOG1, wxID_DIALOG1BUTTONCLEAR, wxID_DIALOG1BUTTONCLOSE, 
 wxID_DIALOG1BUTTONINIT, wxID_DIALOG1BUTTONLOADFROMSERVER, wxID_DIALOG1GRID1, 
 wxID_DIALOG1STATICBOX1, 
] = [wx.NewId() for _init_ctrls in range(7)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(383, 254), size=wx.Size(319, 305),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'Init my battlefield')
        self.SetClientSize(wx.Size(311, 278))
        self.Bind(wx.EVT_INIT_DIALOG, self.OnDialog1InitDialog)

        self.buttonInit = wx.Button(id=wxID_DIALOG1BUTTONINIT, label=u'Init!',
              name=u'buttonInit', parent=self, pos=wx.Point(56, 248),
              size=wx.Size(100, 25), style=0)
        self.buttonInit.SetToolTipString(u'Press the button to send this battlefield to the server')
        self.buttonInit.Bind(wx.EVT_BUTTON, self.OnButtonInitButton,
              id=wxID_DIALOG1BUTTONINIT)

        self.buttonClose = wx.Button(id=wxID_DIALOG1BUTTONCLOSE,
              label=u'Cancel', name=u'buttonClose', parent=self,
              pos=wx.Point(160, 248), size=wx.Size(100, 25), style=0)
        self.buttonClose.SetToolTipString(u'Cancel')
        self.buttonClose.Bind(wx.EVT_BUTTON, self.OnButtonCloseButton,
              id=wxID_DIALOG1BUTTONCLOSE)

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(16, 24), size=wx.Size(280, 176),
              style=0)
        self.grid1.SetColLabelSize(0)
        self.grid1.SetColMinimalAcceptableWidth(15)
        self.grid1.SetDefaultRowSize(15)
        self.grid1.SetDefaultColSize(15)
        self.grid1.SetRowLabelSize(0)
        self.grid1.SetCellHighlightROPenWidth(1)
        self.grid1.EnableEditing(False)
        self.grid1.Enable(True)
        self.grid1.SetSelectionForeground(wx.Colour(0, 0, 0))
        self.grid1.SetScrollLineX(15)
        self.grid1.SetScrollLineY(15)
        self.grid1.SetCellHighlightColour(wx.Colour(255, 255, 255))
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
              self.OnGrid1GridCellLeftClick)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
              self.OnGrid1GridCellRightClick)

        self.staticBox1 = wx.StaticBox(id=wxID_DIALOG1STATICBOX1,
              label=u'Battlefield: try left-click, right-click',
              name='staticBox1', parent=self, pos=wx.Point(8, 2),
              size=wx.Size(296, 240), style=0)

        self.buttonClear = wx.Button(id=wxID_DIALOG1BUTTONCLEAR,
              label=u'Clear all', name=u'buttonClear', parent=self,
              pos=wx.Point(221, 208), size=wx.Size(75, 23), style=0)
        self.buttonClear.SetToolTipString(u'Clears battlefield')
        self.buttonClear.Bind(wx.EVT_BUTTON, self.OnButtonClearButton,
              id=wxID_DIALOG1BUTTONCLEAR)

        self.buttonLoadFromServer = wx.Button(id=wxID_DIALOG1BUTTONLOADFROMSERVER,
              label=u'Refresh', name=u'buttonLoadFromServer', parent=self,
              pos=wx.Point(16, 208), size=wx.Size(80, 23), style=0)
        self.buttonLoadFromServer.SetToolTipString(u'Reloads information from server')
        self.buttonLoadFromServer.Bind(wx.EVT_BUTTON,
              self.OnButtonLoadFromServerButton,
              id=wxID_DIALOG1BUTTONLOADFROMSERVER)

    def __init__(self, parent,app):
        self._init_ctrls(parent)
        self.grid1_created = False
        self.app = app
        self.SetTitle( self.app.player_name + ': battlefield init' )
                
    def OnButtonCloseButton(self, event):
        self.Show(False)

    def OnDialog1InitDialog(self, event):
        
        if( self.grid1_created == False ):
            self.grid1.CreateGrid( 10 , 10, 0 )

            self.grid1.DisableDragRowSize()
            self.grid1.DisableDragColSize()
            self.grid1_created = True
        self.OnButtonLoadFromServerButton( None )
        

    def OnButtonClearButton(self, event):
        for i in range( 0 , 10 ):
            for j in range( 0 , 10 ):
                self.grid1.SetCellBackgroundColour( i , j , wx.WHITE )
        self.grid1.ForceRefresh()

    def OnGrid1GridCellLeftClick(self, event):
        col = event.GetCol()
        row = event.GetRow()
        self.grid1.SetCellBackgroundColour( row , col , wx.BLACK )
        self.grid1.ForceRefresh()
        event.Skip()

    def OnGrid1GridCellRightClick(self, event):
        col = event.GetCol()
        row = event.GetRow()
        self.grid1.SetCellBackgroundColour( row , col , wx.WHITE )
        self.grid1.ForceRefresh()

    def OnButtonInitButton(self, event):
        
        battlefield_str = ''
        for i in range( 0 , CONFIG_BATTLE_FIELD_VER_SIZE ):
            for j in range( 0 , CONFIG_BATTLE_FIELD_HOR_SIZE ):
                color = self.grid1.GetCellBackgroundColour( i , j )
                if( color == wx.BLACK ):
                    sq = 's' 
                else:
                    sq = 'o'
                battlefield_str = battlefield_str + sq

        if( self.app.server_proxy.init_battlefield( battlefield_str ) ):
            self.Close()

    def OnButtonLoadFromServerButton(self, event):
        battlefield_str = self.app.server_proxy.get_battlefield()
        
        for i in range( 0 , CONFIG_BATTLE_FIELD_VER_SIZE ):
            for j in range( 0 , CONFIG_BATTLE_FIELD_HOR_SIZE ):
                if( battlefield_str[ j + i * CONFIG_BATTLE_FIELD_HOR_SIZE ] == 's' ):
                    color = wx.BLACK
                else:
                    color = wx.WHITE
                    
                color = self.grid1.SetCellBackgroundColour( i , j , color)
        self.grid1.ForceRefresh()


